/**
 * Copyright (C) 2010 Telecom Digital
 */
package org.achartengine.chartdemo.demo;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.TimeChart;
import org.achartengine.chartdemo.demo.chart.IChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import cc.mango.android.dto.StkIntraday;
import cc.mango.common.FinalKey;
import cc.mango.common.StringUtils;
import cc.mango.data.DataHelper;
import cc.telecomdigital.tdstock.activity.chart.NvChart;

public class GeneratedChartDemo extends ListActivity
{
	//series number
	private static final int	SERIES_NR		= 1;

	private String			stockCode;

	private String[]		mMenuText;

	private String[]		mMenuSummary;

	private DataHelper		dataHelper	= new DataHelper();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// I know, I know, this should go into strings.xml and accessed using
		// getString(R.string....)
		mMenuText = new String[] { "Time chart", "Bar chart",
				"Candlestick chart", "Nominal & Volume" };

		mMenuSummary = new String[] {
				"Time chart with Intraday(Nominal) values",
				"Bar chart with Intraday(Volume) values",
				"Candlestick chart with Daily values",
				"Nominal & Volume Chart" };

		stockCode = this.getIntent().getStringExtra(FinalKey.ITEMCODE);

		setListAdapter(new SimpleAdapter(this, getListValues(),
				android.R.layout.simple_list_item_2, new String[] {
						IChart.NAME, IChart.DESC }, new int[] {
						android.R.id.text1, android.R.id.text2 }));
	}

	private List<Map<String, String>> getListValues()
	{
		List<Map<String, String>> values = new ArrayList<Map<String, String>>();
		int length = mMenuText.length;
		for (int i = 0; i < length; i++)
		{
			Map<String, String> v = new HashMap<String, String>();
			v.put(IChart.NAME, mMenuText[i]);
			v.put(IChart.DESC, mMenuSummary[i]);
			values.add(v);
		}
		return values;
	}

	private XYMultipleSeriesDataset getDemoDataset()
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		final int nr = 100;
		Random r = new Random();
		for (int i = 0; i < SERIES_NR; i++)
		{
			XYSeries series = new XYSeries("Series " + (i + 1));
			for (int k = 0; k < nr; k++)
			{
				series.add(k, 20 + r.nextInt() % 100);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}

	private XYMultipleSeriesDataset getDateDemoDataset()
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		final int nr = 100;

		long value = new Date().getTime() - 3 * TimeChart.DAY;

		Random r = new Random();
		for (int i = 0; i < SERIES_NR; i++)
		{
			TimeSeries series = new TimeSeries("Series " + (i + 1));

			for (int k = 0; k < nr; k++)
			{
				series.add(new Date(value + k * TimeChart.DAY / 4), 20 + r
						.nextInt() % 100);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}

	private XYMultipleSeriesDataset getDatasetByStockCode(String stockCode)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		String seriesTitle = "";
		//String seriesTitle = "Stock[ "+stockCode+" ]";
		TimeSeries series = new TimeSeries(seriesTitle);
		List<StkIntraday> stkList = null;
		try
		{
			stkList = dataHelper.getStkIntraListByCode(stockCode,
					FinalKey.INTRADAY_DATETYPE_TODAY, /*"", "","",""*/
					GeneratedChartDemo.this, "");
		}
		catch (SocketTimeoutException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < stkList.size(); i++)
		{
			StkIntraday dto = stkList.get(i);
			Date xDate = null;
			String xValue = dto.getDtyyyymmdd() + dto.getTime();

			xDate = StringUtils.string2Date(xValue,
					FinalKey.YEAR_MONTH_DAT_TIME_FORMAT);

			series.add(xDate, dto.getClose());
		}

		dataset.addSeries(series);

		return dataset;
	}

	private XYMultipleSeriesDataset getBarDemoDataset()
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		final int nr = 100;
		Random r = new Random();
		for (int i = 0; i < SERIES_NR; i++)
		{
			CategorySeries series = new CategorySeries("Series " + (i + 1));
			for (int k = 0; k < nr; k++)
			{
				series.add(100 + r.nextInt() % 100);
			}
			dataset.addSeries(series.toXYSeries());
		}
		return dataset;
	}

	private XYMultipleSeriesRenderer getDemoRenderer()
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

		//XYSeriesRenderer a = new XYSeriesRenderer();
		//a.setColor(Color.BLUE);
		// a.setPointStyle(PointStyle.SQUARE);
		//a.setFillBelowLine(true);
		//a.setFillBelowLineColor(Color.WHITE);
		//a.setFillPoints(true);
		//renderer.addSeriesRenderer(a);

		XYSeriesRenderer b = new XYSeriesRenderer();
		b.setPointStyle(PointStyle.POINT);
		b.setColor(Color.GREEN);
		b.setFillPoints(true);

		b.setFillBelowLineColor(Color.LTGRAY);
		renderer.addSeriesRenderer(b);
		renderer.setShowLegend(false);
		renderer.setShowLabels(false);
		renderer.setShowAxes(false);
		renderer.setShowGrid(false);
		renderer.setAxesColor(Color.DKGRAY);
		renderer.setLabelsColor(Color.LTGRAY);
		renderer.setLegendSize(0);
		return renderer;
	}

	public XYMultipleSeriesRenderer getBarDemoRenderer()
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setColor(Color.BLUE);
		renderer.addSeriesRenderer(r);
		r = new SimpleSeriesRenderer();
		r.setColor(Color.GREEN);
		renderer.addSeriesRenderer(r);
		return renderer;
	}

	private void setChartSettings(XYMultipleSeriesRenderer renderer)
	{
		renderer.setChartTitle("Chart demo");
		renderer.setXTitle("x values");
		renderer.setYTitle("y values");
		renderer.setXAxisMin(0.5);
		renderer.setXAxisMax(10.5);
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(210);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		Intent intent = null;
		switch (position)
		{
			case 0:
				XYMultipleSeriesDataset seriesDataset = getDatasetByStockCode(stockCode);
				XYMultipleSeriesRenderer seriesRenderer = getDemoRenderer();
				String dateFormat = FinalKey.HH_MM_FORMAT;
				intent = ChartFactory.getTimeChartIntent(this,
						seriesDataset, seriesRenderer, dateFormat);
				startActivity(intent);
				break;
			case 1:
				//    		VolumeBarChart barChart = new VolumeBarChart(stockCode);
				//    		intent = barChart.execute(this);
				//    		//XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
				//    		//setChartSettings(renderer);
				//    		//intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
				//    		startActivity(intent);
				break;
			case 2:
				//    		CsChart csChart = new CsChart(stockCode);
				//    		intent = csChart.execute(this);
				//    		//XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
				//    		//setChartSettings(renderer);
				//    		//intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
				//    		startActivity(intent);
				break;
			case 3:
				intent = new Intent(this, NvChart.class);
				intent.putExtra(FinalKey.ITEMCODE, stockCode);
				startActivity(intent);
				break;
		}
	}
}